package com.jy.modules.cims.component.credit.credit;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.exception.CimsException;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.component.creditRight.plan.CreditPlanConstant;
import com.jy.modules.cims.data.common.dao.TCMmCostPlanDao;
import com.jy.modules.cims.data.common.dao.TCMmCreditSettleParamDao;
import com.jy.modules.cims.data.common.dto.TCMmCostPlanDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditSettleParamDTO;
import com.jy.modules.cims.data.dao.CostPlanDao;
import com.jy.modules.cims.data.dto.credit.CreditFeeDto;
import com.jy.modules.cims.data.dto.credit.CreditFeeParamDto;

/**
 * @description 债权费用计算实现组件。
 * @author shengzhoutao
 * @date 2015-08-11
 */
@Service("com.jy.modules.cims.component.credit.credit.CreditFeeImpl")
public class CreditFeeImpl implements ICreditFee {
	@Autowired
	private TCMmCreditSettleParamDao tCMmCreditSettleParamDao;

	@Autowired
	private TCMmCostPlanDao tCMmCostPlanDao;

	@Autowired
	private CostPlanDao costPlanDao;

	/**
	 * @description 正常结算债权各项费用
	 * @param creditFeeParamDto
	 *            债权费用计算参数对象<br/>
	 *            债权转让编号：transferId；合约编号：contractCode；当前期次：currentTime， 以上
	 *            三个字段的值为必输入项。
	 *            如何债权结算参数多条，月还还款方式:repayType 的值为必输入项，用此来确定唯一一条数据。
	 * @return CreditFeeDto 债权费用对象，计算后的各项费用信息。
	 * @author shengzhoutao
	 * @date 2015-08-11
	 */
	public CreditFeeDto getNormalSettleInfo(final CreditFeeParamDto creditFeeParamDto) {
		TCMmCreditSettleParamDTO tCMmCreditSettleParamDTO = new TCMmCreditSettleParamDTO();
		tCMmCreditSettleParamDTO.setContractCode(creditFeeParamDto.getContractCode());
		tCMmCreditSettleParamDTO.setRepayType(creditFeeParamDto.getRepayType());
		List<TCMmCreditSettleParamDTO> tCMmCreditSettleParamDTOList = tCMmCreditSettleParamDao.searchTCMmCreditSettleParam(SearchParamUtil
				.getSearchParam(tCMmCreditSettleParamDTO));
		if (CollectionsUtils.isEmpty(tCMmCreditSettleParamDTOList)) {
			return null;
		}

		TCMmCostPlanDTO tCMmCostPlanDTO = new TCMmCostPlanDTO();
		tCMmCostPlanDTO.setTransferId(creditFeeParamDto.getTransferId());
		tCMmCostPlanDTO.setCurrentTime(creditFeeParamDto.getCurrentTime());
		List<TCMmCostPlanDTO> tCMmCostPlanDTOList = tCMmCostPlanDao.searchTCMmCostPlan(SearchParamUtil.getSearchParam(tCMmCostPlanDTO));
		if (CollectionsUtils.isEmpty(tCMmCostPlanDTOList)) {
			return null;
		}

		CreditFeeDto creditFeeDto = getSettleInfo(tCMmCreditSettleParamDTOList.get(0), tCMmCostPlanDTOList.get(0));
		tCMmCreditSettleParamDTO = null;
		tCMmCostPlanDTO = null;

		return creditFeeDto;
	}

	/**
	 * @description 异常结算债权各项费用
	 * @param creditFeeParamDto
	 *            债权费用计算参数对象<br/>
	 *            债权转让编号：transferId；合约编号:contractCode；当前期次:currentTime，
	 *            结算状态：settleStatus
	 *            （01-提前还款），以上 四个字段的值为必输入项。
	 *            如何债权结算参数多条，月还还款方式:repayType 的值为必输入项，用此来确定唯一一条数据。
	 * @return CreditFeeDto 债权费用对象，计算后的各项费用信息。
	 * @author shengzhoutao
	 * @date 2015-08-11
	 */
	public CreditFeeDto getAbnormalSettleInfo(final CreditFeeParamDto creditFeeParamDto) {
		TCMmCreditSettleParamDTO tCMmCreditSettleParamDTO = new TCMmCreditSettleParamDTO();
		tCMmCreditSettleParamDTO.setContractCode(creditFeeParamDto.getContractCode());
		tCMmCreditSettleParamDTO.setRepayType(creditFeeParamDto.getRepayType());
		List<TCMmCreditSettleParamDTO> tCMmCreditSettleParamDTOList = tCMmCreditSettleParamDao.searchTCMmCreditSettleParam(SearchParamUtil
				.getSearchParam(tCMmCreditSettleParamDTO));
		if (CollectionsUtils.isEmpty(tCMmCreditSettleParamDTOList)) {
			return null;
		}

		CreditFeeDto creditFeeDto = null;
		TCMmCostPlanDTO tCMmCostPlanDTO = null;
		if (CreditPlanConstant.DEAL_MODE_ADVANCEFINISH.equals(creditFeeParamDto.getSettleStatus())) {
			tCMmCostPlanDTO = new TCMmCostPlanDTO();
			tCMmCostPlanDTO.setTransferId(creditFeeParamDto.getTransferId());
			tCMmCostPlanDTO.setCurrentTime(creditFeeParamDto.getCurrentTime());
			tCMmCostPlanDTO = costPlanDao.statCostPlanForAdvanceFinish(SearchParamUtil.getSearchParam(tCMmCostPlanDTO));

			creditFeeDto = getSettleInfo(tCMmCreditSettleParamDTOList.get(0), tCMmCostPlanDTO);
	
			tCMmCostPlanDTO = null;
			tCMmCreditSettleParamDTO = null;
	    }
		return creditFeeDto;
	}

	// -------------------------------------------------------------------------------------
	/**
	 * @description 根据债权结算参数和结算计划，计算计算费用
	 * @param tCMmCreditSettleParamDTO
	 *            债权结算参数
	 * @param tCMmCostPlanDTO
	 *            结算计划
	 * @return 债权费用对象
	 */
	public CreditFeeDto getSettleInfo(final TCMmCreditSettleParamDTO tCMmCreditSettleParamDTO, final TCMmCostPlanDTO tCMmCostPlanDTO) {
		CreditFeeDto creditFeeDto = new CreditFeeDto();

		String replaceType = tCMmCreditSettleParamDTO.getReplaceType();// 月还处理方式
		BigDecimal mustAmount = tCMmCostPlanDTO.getMustAmount();// 应还本
		BigDecimal mustAccrual = tCMmCostPlanDTO.getMustAccrual();// 应还息
		BigDecimal thisRepaySum = BigDecimal.ZERO;// 本期还款金额
		BigDecimal matchAmount = BigDecimal.ZERO;// 应匹配金额
		BigDecimal returnAmount = BigDecimal.ZERO;// 应返还金额

		// 全额匹配
		if (CimsConstant.REPLACE_TYPE_FULL_MATCH.equals(replaceType)) {
			matchAmount = mustAmount.add(mustAccrual);
			returnAmount = BigDecimal.ZERO;
		}// 本金匹配
		else if (CimsConstant.REPLACE_TYPE_PRINCIPAL_MATCH.equals(replaceType)) {
			matchAmount = mustAmount;
			returnAmount = mustAccrual;
		}// 全额返还
		else if (CimsConstant.REPLACE_TYPE_FULL_RETURN.equals(replaceType)) {
			matchAmount = BigDecimal.ZERO;
			returnAmount =  mustAmount.add(mustAccrual);;
		}
		//
		else {
			throw new CimsException("月还处理方式(01:全额匹配;02:本金匹配;03:全额返还)值不正确！");
		}

		creditFeeDto.setMustAmount(mustAmount);
		creditFeeDto.setMustAccrual(mustAccrual);
		creditFeeDto.setThisRepaySum(thisRepaySum);
		creditFeeDto.setMatchAmount(matchAmount);
		creditFeeDto.setReturnAmount(returnAmount);

		return creditFeeDto;
	}
}
